package com.example.eventreg.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.eventreg.admin.Admin;
import com.example.eventreg.security.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin")
@CrossOrigin
@RestController // 用于定义该类为一个RESTful API控制器
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtUtil jwtUtil;

    // 构造函数，用于初始化模拟数据
    public AdminController() {
        // 初始化一些用户数据
    }
    @GetMapping("/get")
    public List<Admin> getAllAdmin() {
        return adminMapper.selectList(null); // 返回200状态码和用户列表
    }
    //获取指定学生
    @GetMapping("/{account}")
    public Admin getOneAdmin(@PathVariable String account) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("account",account)); // 返回200状态码和用户列表
    }
    @GetMapping("/self")
    public Admin getOnSelf(@RequestHeader("Authorization") String token) {
        if(token.startsWith("Bearer "))
        {
            token = token.substring(7);
        }
        System.out.println("tokenC: "+token);
        String account = jwtUtil.getUserIdFromToken(token);
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("account",account)); // 返回200状态码和用户列表
    }
    //创建新学生用户
    @PostMapping("/reg")
    public Admin createAdmin(@RequestBody Admin Admin) {
        adminMapper.insert(Admin);
        return Admin;
    }
    // 更新指定ID的用户
    @PutMapping("/update")
    public Integer updateAdmin(@RequestBody Admin updatedAdmin) {
        return adminMapper.updateById(updatedAdmin);
    }
    //删除指定用户
    @PutMapping("/del/{account}")
    public Integer deleteAdmin(@PathVariable String account) {
        return adminMapper.deleteById(account);
    }

}